/**
 * 
 */
package client;

import java.io.IOException;
import java.net.Socket;

import common.CommonFile;
import common.ReceiveFile;
import common.SendFile;

/**
 * @author stephane
 *
 */
public class AnnonceurClientDelegate implements Runnable {
	private SendFile m_sendFile;
	private Socket m_dataSocket;
	private ReceiveFile m_recvFile;
	private boolean m_bFini;
	private int m_TransfertMode;
	
	
	public AnnonceurClientDelegate(Socket mSocket, int i) {
		this.m_dataSocket = mSocket;
		this.m_bFini = false;
		this.m_TransfertMode = CommonFile.NoTransfertMode;
	}

	public SendFile getM_sendFile() {
		return m_sendFile;
	}

	public void setM_sendFile(SendFile mSendFile) {
		m_sendFile = mSendFile;
	}

	public boolean isM_bFini() {
		return m_bFini;
	}

	public void setM_bFini(boolean mBFini) {
		m_bFini = mBFini;
	}

	public int getM_TransfertMode() {
		return m_TransfertMode;
	}

	public void setM_TransfertMode(int mTransfertMode) {
		m_TransfertMode = mTransfertMode;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		while ( m_bFini == false ){
			switch( m_TransfertMode ){
			case CommonFile.SendTranfertMode:
				try {
					m_sendFile.envoieFichier();
					m_TransfertMode = CommonFile.NoTransfertMode;
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			case CommonFile.ReceiveTransfertMode:
				try {
					m_recvFile.recoitFichier();
					m_TransfertMode = CommonFile.NoTransfertMode;
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			case CommonFile.NoTransfertMode:
			default:
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				break;
			}
			}
		try {
			m_dataSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setM_dataSocket(Socket m_dataSocket) {
		this.m_dataSocket = m_dataSocket;
	}

	public Socket getM_dataSocket() {
		return m_dataSocket;
	}

}
